package com.tjsinfo.tjsoa.addresslist.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.addresslist.bo.IAddressListSystemUserBo;
import com.tjsinfo.tjsoa.addresslist.bo.IAddressListUserBo;
import com.tjsinfo.tjsoa.addresslist.bo.IAddressListUserPageBo;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListSystemUser;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.dao.ITSystemDepartmentDao;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;

public class UpdateTjsUserAction extends Action{
	private IAddressListSystemUserBo addressListSystemUserBo;
	public void setAddressListSystemUserBo(
			IAddressListSystemUserBo addressListSystemUserBo) {
		this.addressListSystemUserBo = addressListSystemUserBo;
	}
	
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	private Date date = null;
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		HttpSession session = request.getSession();
		User users = (User) session.getAttribute("user");
		AddressListSystemUser listUser =  addressListSystemUserBo.findByUserId(Integer.valueOf(users.getUser_id()));
		
		String moduleType = request.getParameter("moduleType");
		
		String id = Integer.valueOf(users.getUser_id()).toString();
		String userName = request.getParameter("userName");
		String mobile = request.getParameter("mobile");
		String email = request.getParameter("email");
		
		String homeAddress = request.getParameter("homeAddress");
		String homePhone = request.getParameter("homePhone");
		String homePostal = request.getParameter("homePostal");
		
		String company = request.getParameter("company");
		String sector = request.getParameter("sector");
		String duty = request.getParameter("duty");
		String companyPhone = request.getParameter("companyPhone");
		String companyFax = request.getParameter("companyFax");
		String companyAddress = request.getParameter("companyAddress");
		String companyPostal = request.getParameter("companyPostal");
		
		String birthday = request.getParameter("birthday");
		String qq = request.getParameter("qq");
		String msn = request.getParameter("msn");
		String blog = request.getParameter("blog");
		String remarks = request.getParameter("remarks");
		
		if (mobile == null)mobile="";
		if (email == null)email="";
		if (homeAddress == null)homeAddress="";
		if (homePhone == null)homePhone="";
		if (homePostal == null)homePostal="";
		if (sector == null)sector="";
		if (duty == null)duty="";
		if (companyPhone == null)companyPhone="";
		if (companyFax == null)companyFax="";
		if (companyAddress == null)companyAddress="";
		if (companyPostal == null)companyPostal="";
		if (qq == null)qq="";
		if (msn == null)msn="";
		if (blog == null)blog="";
		if (remarks == null)remarks="";
		
		listUser.setUserName(userName);
		listUser.setMobile(mobile);
		listUser.setEmail(email);
		listUser.setHomeAddress(homeAddress);
		listUser.setHomePhone(homePhone);
		listUser.setHomePostal(homePostal);
		listUser.setCompany(company);
		listUser.setSector(sector);
		listUser.setDuty(duty);
		listUser.setCompanyPhone(companyPhone);
		listUser.setCompanyFax(companyFax);
		listUser.setCompanyAddress(companyAddress);
		listUser.setCompanyPostal(companyPostal);
		if (birthday != null && !"".equals(birthday)) {
			try {
				date = sdf.parse(birthday);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			listUser.setBirthday(date);
		}
		listUser.setQq(qq);
		listUser.setMsn(msn);
		listUser.setBlog(blog);
		if (remarks != null && !"".equals(remarks)) {
			listUser.setRemarks(remarks.trim());
		}
		if(listUser.getId()==null){
			listUser.setUserId(users.getUser_id());
			addressListSystemUserBo.save(listUser);
		}else {
			addressListSystemUserBo.updates(id,Integer.valueOf(users.getUser_id()).toString(),userName, mobile, email, company, sector, duty, companyPhone, companyFax, companyAddress, companyPostal, homeAddress, homePostal, homePhone, birthday, qq, msn, blog, remarks);
		}
		
		request.setAttribute("moduleType", moduleType);
		request.setAttribute("listUser", listUser);
		request.setAttribute("returnString", "修改成功");
		return mapping.findForward("ok");
	}
}
